<?php
namespace app\model;
use think\Model;


class GoodsCategory extends Model
{
    protected $name = 'goods_category';
    /**
     * 获取商品分类列表
     */
    public function  goodsCategoryList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $cateList=[];
        $list = self::where(['is_del'=>0])->select();
        if (!empty($list)){
            $cateList = listCategories($list->toArray());
        }
        $result = [
            'list'=>$cateList,
        ];
        return getSuccessResponse('查询成功',$result);
    }

    /**
     * 新增编辑商品分类
     */
    public function  addEditCate($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        $name = $param['name'] ?? '';
        $parent_id = $param['parent_id'] ?? 0;
        $remark = $param['remark'] ?? '';
        $status = $param['status'] ?? 1;
        $image = $param['image'] ?? '';
        if (empty($name)){
            return getFailResponse('参数有误');
        }
        $data = [
            'name'=>$name,
            'parent_id'=>$parent_id,
            'remark'=>$remark,
            'status'=>$status,
            'image'=>$image,
            'create_time'=>time(),
            'update_time'=>time(),
        ];
        if (empty($id)){
            GoodsCategory::create($data);
        }else{
            unset($data['create_time']);
            GoodsCategory::where(['id'=>$id])->update($data);
        }
        return getSuccessResponse();
    }

    /**
     * 删除商品分类
     */
    public function  delGoodsCate($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $ids = $param['ids'] ?? '';
        if (empty($ids)){
            return getFailResponse('参数有误');
        }
        self::where('id','in',$ids)->update(['is_del'=>1,'update_time'=>time()]);
        return getSuccessResponse('删除成功');
    }

    /**
     * 是否启用商品分类
     */
    public function  enableGoodsCate($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $status = $param['status'] ?? '';
        $id = $param['id'] ?? '';
        if (empty($id)  || !in_array($status,[1,0])){
            return getFailResponse('参数有误');
        }
        self::where(['id'=>$id])->update(['status'=>$status,'update_time'=>time()]);
        return getSuccessResponse();
    }
}